Skip to content

Fix #5046: Avoid fishy reference equality on strings #5049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2018

Conversation

Blaisorblade
Copy link
Contributor

@Blaisorblade Blaisorblade commented Aug 27, 2018

While the code looks correct (because strippedName comes from stripSuffix and because of stripSuffix's contract and implementation), this is subtle enough that we should either add a test or use !=.

Since String.equals happens to be O(1) in this case, changing the code looks simpler. String.equals is O(1) here because it will either succeed by testing pointer equality or fail by noticing a length mismatch, so it will never look at the actual contents!

Fix #5046.

While the code looks correct (because strippedName comes from stripSuffix and because of stripSuffix's contract and implementation), this is subtle enough that we should either add a test or use !=.

Since `String.equals` happens to be O(1) in this case, changing the code looks simpler. `String.equals` is O(1) here because it will either succeed by testing pointer equality or fail by noticing a length mismatch, so it will never look at the actual contents!
@Blaisorblade Blaisorblade changed the title Avoid fishy reference equality on strings Fix #5046: Avoid fishy reference equality on strings Aug 27, 2018
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@liufengyun liufengyun merged commit 3a5460b into scala:master Aug 28, 2018
@liufengyun liufengyun deleted the avoid-fishy-string-ne branch August 28, 2018 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suspicious comparison of strings by reference identity
2 participants